//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		account:app.setting.account,
		pagetitle:'申请退款',
		
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			console.log('refund');console.log(pagedata);
			if(pagedata.item){
				pagedata.item.needchecked=1;
			}
			page.setData({
				pagedata:pagedata
			});
			if(app.access.op=='list'){
				setTimeout(()=>{page.getlist(1);},500);
			}
		});
	},
	getlist:function(e){
		var the=this
		var data={}
		var page=the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		pagedata.list=pagedata.list?pagedata.list:[]
		if(e){
			if(e.currentTarget){
				data = e.currentTarget.dataset;
				pagedata.params.status=data.status
			}
			pagedata.list=[]
			page=1
			isnodata=false;
		}else{
			if(isnodata){
				return false;
			}
		}
		var userdeleted=data.userdeleted?data.userdeleted:'';
		app.util.get('/order.refund/getlist/status='+pagedata.params.status+'&page='+page,function(res){
			if(res=='none'){
				the.setData({
					isnodata:true,
					'pagedata.list':pagedata.list,
					'pagedata.params.status':pagedata.params.status,
					'pagedata.userdeleted':userdeleted,
					loading:false,
				});
			}else{
				pagedata.list = pagedata.list.concat(res)
				page++
				the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.params.status':pagedata.params.status,
					'pagedata.userdeleted':userdeleted,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
		});
	},
	//翻页
	onReachBottom: function () {
		var isnodata=this.data.isnodata
		var loading=this.data.loading
		if(!isnodata && !loading && app.access.op=='list'){
			this.getlist()
		}
	},
	imagedel: function(e){
		var the = this
		var pagedata = the.data.pagedata;
		var index = e.currentTarget.dataset.index
			pagedata.item.images.splice(index,1);
			the.setData({
			  'pagedata.item.images':pagedata.item.images
			})
	},
	chooseImage:function(e){
		var that =this
		var the =this
		var pagedata =the.data.pagedata	
		var images=pagedata.item.images
		app.util.showLoading('上传中...');
		app.util.up_file('image',function(res,thumb){
			app.util.hideLoading()
			images=images?images:[]
			images.push(res);
			the.setData({
				'pagedata.item.images': images,
			})	
		});
	},
	choosevideo:function(e){
		var the =this
		app.util.video(function(video,image){
			the.setData({
				'pagedata.item.videos':video,
			})
		})
	},
	msgtypes:function(e){
		var the = this
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		pagedata.setmsgtypes=pagedata.setmsgtypes?pagedata.setmsgtypes:{};
		pagedata.setmsgtypes[data.index]=pagedata.setmsgtypes[data.index]?0:1;
		var duoxuan = pagedata.item.msgtypes?pagedata.item.msgtypes:';';
			if(duoxuan.indexOf(';'+data.value+';')<0){
				duoxuan=duoxuan+data.value+';';
			}else{
				duoxuan=duoxuan.replace(';'+data.value+';',';');
				if(duoxuan==';'){
					duoxuan='';
				}
			}
		console.log(pagedata.setmsgtypes)
		the.setData({
			'pagedata.item.msgtypes': duoxuan,
			'pagedata.setmsgtypes': pagedata.setmsgtypes,
		})
	},
	setcontent:function(e){
		var that =this
		that.setData({
			'pagedata.item.content': e.detail.value,
	    })
	},
	applytype:function(e){
		var the=this;
		var value=e.detail.value;
		the.setData({
			'pagedata.item.type':value,
		});
	},
	setprice:function(e){
		var that =this
		that.setData({
			'pagedata.item.price': e.detail.value,
	    })
	},
	setsn:function(e){
		var that =this
		that.setData({
			'pagedata.item.sn': e.detail.value,
	    })
	},
	setgoodsnum:function(e){
		var that =this
		that.setData({
			'pagedata.item.goodsnum': e.detail.value,
	    })
	},
	applypost:function(){
		var that =this
		var data={};
		var pagedata=that.data.pagedata
		app.util.post('jy_weishop/order.refund/apply/',pagedata.item);
	},
	sendpost:function(){
		var that =this
		var data={};
		var pagedata=that.data.pagedata
		app.util.post('jy_weishop/order.refund/send/',pagedata.item);
	},
	expressChange: function(e) {
		//console.log('picker发送选择改变，携带值为', e.detail.value)
		var that =this
		var pagedata=that.data.pagedata
		that.setData({
			'pagedata.item.com': pagedata.express_types[e.detail.value].name,
	    })
	  },
	setshow_xieyi:function(){
		var the=this;
		var pagedata=the.data.pagedata;
		var show_xieyi=pagedata.show_xieyi?false:true;
		the.setData({
			'pagedata.show_xieyi':show_xieyi
		})
	},
	setcheck:function(e){
		var the=this;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata;
		var checked=pagedata.item.checked?false:true;
		if(data.type){
			checked=true;
		}
		the.setData({
			'pagedata.item.checked':checked,
			'pagedata.show_xieyi':false
		})
	},
	previewimage: function(e){
		var data=e.currentTarget.dataset;
		var image=image?image:''
		var images=data.images? data.images:[]
			wx.previewImage({
			  current: image, // 当前显示图片的http链接
			  urls:  images // 需要预览的图片http链接列表
			})
	},
	cancel:function(e){
		var data=e.currentTarget.dataset;
		app.util.post('jy_weishop/order.refund/cancel/',data);
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('jy_weishop/order.refund/post/',data);
	}
});